package asteroids;

public class Util {
  public static final double EPSILON = 0.0001;

  public static boolean fuzzyEquals(double x, double y) {
    if (Double.isNaN(x) || Double.isNaN(y))
      return false;
    return Math.abs(x - y) <= EPSILON || Double.valueOf(x).equals(Double.valueOf(y));
  }

  public static boolean fuzzyLessThanOrEqualTo(double x, double y) {
    if (fuzzyEquals(x, y)) {
      return true;
    } else {
      return Double.compare(x, y) < 0;
    }
  }
  
  public static double absoluteError(double expected, double actual) {
    return Math.abs(expected - actual);
  }

  public static double relativeError(double expected, double actual) {
    return absoluteError(expected, actual) / Math.abs(expected);
  }
  
	/**
	 * 
	 * @param dimension
	 * @return	...
	 * 			| return (dimension>=getLowerBound() && dimension<= getUpperBound() && !Double.isNan(dimension) && dimension!=null;
	 */
	public static boolean isLocatedWithinRange(double minimum,double maximum, double value){
		if(Util.fuzzyLessThanOrEqualTo(value, maximum)){			//Smaller than maximum
			if(Util.fuzzyEquals(value, minimum))					//equal to minimumvalue
				return true;
			else if(!Util.fuzzyLessThanOrEqualTo(value, minimum))	//Greater than minimum
				return true;
			return false;
		}
		return false;
	}
  
}